package com.czwj.action;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.Camera;
import com.czwj.entities.OperateResult;
import com.dahua.openapi.business.impl.UserManagerImpl;

/**
* @ClassName: DahuaAction
* @Description: TODO
* @author H
* @date 2020年6月1日
*
*/
@Controller
public class DahuaAction{
	@RequestMapping(value = "/openapi.do",method = RequestMethod.POST)
	@ResponseBody
	public String CameraSave(){
		// 注：执行main函数前请先将CONST.java文件中的APPID、SECRET以及PHONE填全，不然程序执行将会报错。
		// 获取管理员token
		UserManagerImpl userManager = new UserManagerImpl();
		HashMap<String,Object> paramsMap = new HashMap<String,Object>();
		JSONObject json = userManager.accessToken(paramsMap);
		JSONObject jsonResult = json.getJSONObject("result");
		JSONObject jsonData = jsonResult.getJSONObject("data");
		String token = jsonData.getString("accessToken");
		return token;
	}
	
	@RequestMapping(value = "/openapicamera.do",method = RequestMethod.POST)
	@ResponseBody
	public String openapicamera(@RequestParam(value = "token", required = false) String token,
			@RequestParam(value = "deviceId", required = false) String deviceId,
			@RequestParam(value = "direction", required = false) String direction) throws UnsupportedEncodingException{
		String nonce = UUID.randomUUID().toString().replaceAll("-","");
		long time = System.currentTimeMillis() / 1000;
		String appSecret = "65724bcf1b81415192737466961470";
		String ver = "1.0";
		String appId = "lc6cf0cecc6ad74a84";
		String signstr = "time:" + time + ",nonce:" + nonce + ",appSecret:" + appSecret;
		String sign = DigestUtils.md5DigestAsHex(signstr.getBytes("utf-8"));
		String id = UUID.randomUUID().toString();
		JSONObject object = new JSONObject();
		JSONObject system = new JSONObject();
		system.put("ver",ver);
		system.put("sign",sign);
		system.put("appId",appId);
		system.put("time",time);
		system.put("nonce",nonce);
		JSONObject params = new JSONObject();
		params.put("deviceId",deviceId);
		params.put("token",token);
		params.put("channelId",0);
		params.put("direction",direction);
		/*params.put("duration",new Long(2000));
		params.put("operation",8);*/
		object.put("system",system);
		object.put("params",params);
		object.put("id",id);
		return object.toString();
	}
	

	@RequestMapping(value = "/cameraSnap.do",method = RequestMethod.POST)
	@ResponseBody
	public String cameraSnap(@RequestParam(value = "token", required = false) String token,
			@RequestParam(value = "deviceId", required = false) String deviceId) throws UnsupportedEncodingException{
		String nonce = UUID.randomUUID().toString().replaceAll("-","");
		long time = System.currentTimeMillis() / 1000;
		String appSecret = "65724bcf1b81415192737466961470";
		String ver = "1.0";
		String appId = "lc6cf0cecc6ad74a84";
		String signstr = "time:" + time + ",nonce:" + nonce + ",appSecret:" + appSecret;
		String sign = DigestUtils.md5DigestAsHex(signstr.getBytes("utf-8"));
		String id = UUID.randomUUID().toString();
		JSONObject object = new JSONObject();
		JSONObject system = new JSONObject();
		system.put("ver",ver);
		system.put("sign",sign);
		system.put("appId",appId);
		system.put("time",time);
		system.put("nonce",nonce);
		JSONObject params = new JSONObject();
		params.put("deviceId",deviceId);
		params.put("token",token);
		params.put("channelId",0);
		object.put("system",system);
		object.put("params",params);
		object.put("id",id);
		return object.toString();
	}
}
